import { Response } from "express";
import { ApiResponse } from "../model/types";

// 成功响应
export const successResponse = <T>(
  res: Response,
  data: T,
  message: string = "操作成功",
  statusCode: number = 200
): void => {
  const response: ApiResponse<T> = {
    success: true,
    data,
    message,
  };
  res.status(statusCode).json({
    data: {
      ...response,
    },
  });
};

// 错误响应
export const errorResponse = (
  res: Response,
  message: string = "操作失败",
  statusCode: number = 400,
  error?: string
): void => {
  const response: ApiResponse = {
    success: false,
    message,
    error,
  };
  res.status(statusCode).json({
    data: {
      ...response,
    },
  });
};

// 分页响应
export const paginatedResponse = <T>(
  res: Response,
  data: T[],
  pagination: {
    page: number;
    pageSize: number;
    total: number;
    totalPages: number;
  },
  message: string = "获取成功"
): void => {
  const response: ApiResponse<T[]> = {
    success: true,
    data,
    message,
    pagination,
  };
  res.json({
    data: {
      ...response,
    },
  });
};
